package com.tianqu.tms.www.settle.controller;

import java.util.Arrays;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TmsTransferFinanceDetailDao;
import com.tianqu.tms.dao.TmsTransferFinanceDetailDaoImp;
import com.tianqu.tms.dao.TransferSettlementInfoDao;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferSettlementDate;
import com.tianqu.tms.entity.TransferSettlementDay;
import com.tianqu.tms.entity.TransferSettlementInfo;
import com.tianqu.tms.www.settle.viewhelper.TransferSettleViewHelper;

public class TransferSettleSearchDisPatchController extends TmsController {
	private Logger logger = Logger
			.getLogger(TransferSettleSearchDisPatchController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		// 添加事务提交异常捕获
		try {
			controller.begin();
			
			TransferSettleViewHelper view = (TransferSettleViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			TmsUserProfile profile = uc.getUserProfile();
			//
			TransferSettlementInfoDao uDao = (TransferSettlementInfoDao) TmsDao.getInstance(TmsDao.TMS_TRANSFER_SETTLEMENT_INFO);
			boolean flag = uDao.isSettled(Integer.parseInt(view.getSettleId()), profile.getUniqueId());
			if(flag) return ;
			
			//根据日期查询
			TransferSettlementDay day = new TransferSettlementDay();
			String endDate = view.getEndDate();
			day = day.getSettleDayInfoByEndDate(endDate);
			int settleDayId = 0;
			if(day!=null)
			{
				settleDayId = day.getId();
				String insertUserId = day.getSettleUserId();
				int userIdBs = 0;
				//把这个userID打成列表
				String[] bb = insertUserId.split(",");
				List yyList = Arrays.asList(bb);	
				for(int i=0;i<yyList.size();i++)
				{
					String uid = (String)yyList.get(i);
					if(uid.equals(String.valueOf(profile.getUniqueId())))
					{
						userIdBs = 1;
					}
				}
				if(userIdBs!=1)
				{
					insertUserId = insertUserId+","+profile.getUniqueId();
					//开始更新
					day.updateSettleIdById(day.getId(),insertUserId);
				}
			}else{
				//插入
				TransferSettlementDay inDay = new TransferSettlementDay();
				inDay.setSettleUserId(String.valueOf(profile.getUniqueId()));
				inDay.setSettleDate(view.getEndDate().substring(0, 10)+" 00:00:00");
				settleDayId = inDay.insertSettlementDayInfo(inDay);
			}
			//将状态2变成3
			TransferSettlementDate upSettle = new TransferSettlementDate();
			upSettle.updateSettleState(profile.getUniqueId());
			//将状态1变成2			
			TransferSettlementDate settleDate = new TransferSettlementDate();
			settleDate = settleDate.getDateInfo(1,profile.getUniqueId());
			TransferSettlementDate updateStatus1 = new TransferSettlementDate();
			updateStatus1.setEndDate(view.getEndDate());
			updateStatus1.setId(settleDate.getId());
			updateStatus1.setSettleState(2);
			updateStatus1.setSettleDayId(settleDayId);
			updateStatus1.updateSettleStateOne(updateStatus1);
			//插入1
			TransferSettlementDate insertEntity = new TransferSettlementDate();
			insertEntity.setStartDate(view.getEndDate());
			insertEntity.setInsertUserId(profile.getUniqueId());
			insertEntity.setSettleState(1);
			insertEntity.insertTransferSettlement(insertEntity);
						
			//流程表收费 查询出时间断内的所有人
//			TransferFinanceInfo finance = new TransferFinanceInfo();
//			List<TransferFinanceInfo> lcList = finance.getFinaneInfoByPeriodLC(view.getStartDate(),view.getEndDate(),profile.getUniqueId());
//			if(lcList!=null)
//			{
//				for(int i=0;i<lcList.size();i++)
//				{
//					TransferFinanceInfo entity = (TransferFinanceInfo)lcList.get(i);
//					TransferSettlementInfo settleInfo = new TransferSettlementInfo();
//					settleInfo.setSettleId(Integer.parseInt(view.getSettleId()));
//					settleInfo.setSettleUserId(profile.getUniqueId());
//					settleInfo.setReceivable(50*entity.getCount());
//					settleInfo.setPaid(entity.getCheckPrice());
//					settleInfo.setInsertUserId(entity.getCheckPriceUserId());
//					settleInfo.setSettletype(1);
//					settleInfo.insertSettleInfo(settleInfo);
//				}
//			}
//
//			//交易票收费
//			List<TransferFinanceInfo> jypList = finance.getFinaneInfoByPeriodJYP(view.getStartDate(),view.getEndDate(),profile.getUniqueId());
//			if(jypList!=null)
//			{
//				for(int i=0;i<jypList.size();i++)
//				{
//					TransferFinanceInfo entity = (TransferFinanceInfo)jypList.get(i);
//					TransferSettlementInfo settleInfo = new TransferSettlementInfo();
//					settleInfo.setSettleId(Integer.parseInt(view.getSettleId()));
//					settleInfo.setSettleUserId(profile.getUniqueId());
//					settleInfo.setReceivable(200*entity.getCount());
//					settleInfo.setPaid(entity.getTransferPrice());
//					settleInfo.setInsertUserId(entity.getTransferPriceUserId());
//					settleInfo.setSettletype(3);
//					settleInfo.insertSettleInfo(settleInfo);
//				}
//			}
//			//待办收费
//			List<TransferFinanceInfo> dbList = finance.getFinaneInfoByPeriodDB(view.getStartDate(),view.getEndDate(),profile.getUniqueId());
//			if(dbList!=null)
//			{
//				for(int i=0;i<dbList.size();i++)
//				{
//					TransferFinanceInfo entity = (TransferFinanceInfo)dbList.get(i);
//					TransferSettlementInfo settleInfo = new TransferSettlementInfo();
//					settleInfo.setSettleId(Integer.parseInt(view.getSettleId()));
//					settleInfo.setSettleUserId(profile.getUniqueId());
//					settleInfo.setReceivable(200*entity.getCount());
//					settleInfo.setPaid(entity.getAgencyPrice());
//					settleInfo.setInsertUserId(entity.getAgencyPriceUserId());
//					settleInfo.setSettletype(6);
//					settleInfo.insertSettleInfo(settleInfo);
//				}
//			}
//			//待办超期/过期收费
//			List<TransferFinanceInfo> dbOutList = finance.getFinaneInfoByPeriodDBOut(view.getStartDate(),view.getEndDate(),profile.getUniqueId());
//			if(dbOutList!=null)
//			{
//				for(int i=0;i<dbOutList.size();i++)
//				{
//					TransferFinanceInfo entity = (TransferFinanceInfo)dbOutList.get(i);
//					TransferSettlementInfo settleInfo = new TransferSettlementInfo();
//					settleInfo.setSettleId(Integer.parseInt(view.getSettleId()));
//					settleInfo.setSettleUserId(profile.getUniqueId());
//					settleInfo.setReceivable(100*entity.getCount());
//					settleInfo.setPaid(entity.getExtendedPrice());
//					settleInfo.setInsertUserId(entity.getExtendedPriceUserId());
//					settleInfo.setSettletype(7);
//					settleInfo.insertSettleInfo(settleInfo);
//				}
//			}
//			//变更交易票收费
//			List<TransferFinanceInfo> bgList = finance.getFinaneInfoByPeriodBG(view.getStartDate(),view.getEndDate(),profile.getUniqueId());
//			if(bgList!=null)
//			{
//				for(int i=0;i<bgList.size();i++)
//				{
//					TransferFinanceInfo entity = (TransferFinanceInfo)bgList.get(i);
//					TransferSettlementInfo settleInfo = new TransferSettlementInfo();
//					settleInfo.setSettleId(Integer.parseInt(view.getSettleId()));
//					settleInfo.setSettleUserId(profile.getUniqueId());
//					settleInfo.setReceivable(200*entity.getCount());
//					settleInfo.setPaid(entity.getChangePrice());
//					settleInfo.setInsertUserId(entity.getChangePriceUserId());
//					settleInfo.setSettletype(5);
//					settleInfo.insertSettleInfo(settleInfo);
//				}
//			}
//			//流程表退费
//			List<TransferFinanceInfo> lcTList = finance.getFinaneInfoByPeriodLCT(view.getStartDate(),view.getEndDate(),profile.getUniqueId());
//			if(lcTList!=null)
//			{
//				for(int i=0;i<lcTList.size();i++)
//				{
//					TransferFinanceInfo entity = (TransferFinanceInfo)lcTList.get(i);
//					TransferSettlementInfo settleInfo = new TransferSettlementInfo();
//					settleInfo.setSettleId(Integer.parseInt(view.getSettleId()));
//					settleInfo.setSettleUserId(profile.getUniqueId());
//					settleInfo.setReceivable(50*entity.getCount());
//					settleInfo.setPaid(entity.getCheckBackPrice());
//					settleInfo.setInsertUserId(entity.getCheckBackPriceUserId());
//					settleInfo.setSettletype(2);
//					settleInfo.insertSettleInfo(settleInfo);
//				}
//			}
//			//交易票退费
//			List<TransferFinanceInfo> jypTList = finance.getFinaneInfoByPeriodJYPT(view.getStartDate(),view.getEndDate(),profile.getUniqueId());
//			if(jypTList!=null)
//			{
//				for(int i=0;i<jypTList.size();i++)
//				{
//					TransferFinanceInfo entity = (TransferFinanceInfo)jypTList.get(i);
//					TransferSettlementInfo settleInfo = new TransferSettlementInfo();
//					settleInfo.setSettleId(Integer.parseInt(view.getSettleId()));
//					settleInfo.setSettleUserId(profile.getUniqueId());
//					settleInfo.setReceivable(200*(entity.getCount()));
//					if(entity.getOtherBackPrice()>0)
//					{
//						settleInfo.setPaid(entity.getOtherBackPrice());
//					}
//					if(entity.getSdBackPrice()>0)
//					{
//						settleInfo.setPaid(entity.getSdBackPrice());
//					}
//					if(entity.getPzBackPrice()>0)
//					{
//						settleInfo.setPaid(entity.getPzBackPrice());
//					}				
//					settleInfo.setInsertUserId(entity.getBackPriceUserId());
//					settleInfo.setSettletype(4);
//					settleInfo.insertSettleInfo(settleInfo);
//				}
//			}
			
			TmsTransferFinanceDetailDao dao = (TmsTransferFinanceDetailDaoImp) TmsDao.getInstance(TmsDao.TMS_TRANSFER_FINANCE_DETAIL);
			List<TransferSettlementInfo> list = dao.getFinaneInfoByPeriod(view.getStartDate(),view.getEndDate(), profile.getUniqueId(), Integer.parseInt(view.getSettleId()), Codes.CHARGE_TYPE_CHARGE);
			for (TransferSettlementInfo transferSettlementInfo : list) transferSettlementInfo.insertSettleInfo(transferSettlementInfo);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		} finally {
			try {
				controller.commit();
			}catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");

	}
}
